package com.opdar.athena.client.agent.controllers;


import com.opdar.athena.base.RedisSessionManager;
import com.opdar.athena.client.agent.annotations.Commond;
import com.opdar.athena.client.agent.annotations.MessageType;
import com.opdar.athena.client.agent.base.Eachback;
import com.opdar.athena.client.agent.base.TcpSessionManager;
import com.opdar.athena.client.agent.server.IoSession;
import com.opdar.athena.entities.UserMigrateEntity;
import com.opdar.athena.mapper.MessageMapper;
import com.opdar.athena.messages.MigrateMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by shiju on 2017/6/29.
 */
@MessageType(value = MigrateMessage.class,type = com.opdar.athena.base.MessageType.MIGRATE)
public class MigrateController {
    @Autowired
    TcpSessionManager tcpSessionManager;
    @Autowired
    RedisSessionManager redisSessionManager;

    @Autowired
    MessageMapper messageMapper;
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Commond("Migrate")
    public Object migrate(IoSession session, final MigrateMessage migrateMessage) {
        logger.info("迁移至目标服务器[{}]",migrateMessage.getDestination());
        switch (migrateMessage.getType()){
            case 1:
                logger.info("整体迁移，开始通知所有用户尝试迁移");
                tcpSessionManager.each(new Eachback<String, IoSession>() {
                    @Override
                    public void get(String key, IoSession value) {
                        if(redisSessionManager.exist(value.getToken())){
                            redisSessionManager.migrate(migrateMessage.getId(),value.getToken());
                            write(value,migrateMessage);
                        }
                    }
                });
                break;
            case 2:
                logger.info("随机迁移，开始通知[{}]名用户尝试随机迁移",migrateMessage.getNum());
                tcpSessionManager.each(new Eachback<String, IoSession>() {
                    @Override
                    public void get(String key, IoSession value) {
                        if(redisSessionManager.exist(value.getToken())){
                            redisSessionManager.migrate(migrateMessage.getId(),value.getToken());
                            write(value,migrateMessage);
                        }
                    }
                },migrateMessage.getNum());
                break;
            case 3:
                logger.info("用户[{}]尝试迁移",migrateMessage.getToken());
                if(redisSessionManager.exist(migrateMessage.getToken())) {
                    redisSessionManager.migrate(migrateMessage.getId(), migrateMessage.getToken());
                    IoSession target = tcpSessionManager.get(migrateMessage.getToken());
                    write(target,migrateMessage);
                }
                break;
        }
        return null;
    }

    private void write(IoSession session,MigrateMessage migrateMessage){
        UserMigrateEntity migrateEntity = new UserMigrateEntity();
        migrateEntity.setId(migrateMessage.getId());
        migrateEntity.setDestination(migrateMessage.getDestination());
        session.write(com.opdar.athena.base.MessageType.MIGRATE,migrateEntity);
    }
}
